package orderoid.soap.Tool;


import java.util.ArrayList;
import java.util.List;

import orderoid.soap.classeCommune.Adresse;
import orderoid.soap.classeCommune.Civilite;
import orderoid.soap.classeCommune.Client;
import orderoid.soap.classeCommune.Commande;
import orderoid.soap.classeCommune.Employe;
import orderoid.soap.classeCommune.MarqueBlanche;
import orderoid.soap.classeCommune.Menu;
import orderoid.soap.classeCommune.Produit;
import orderoid.soap.classeCommune.Restaurant;
import orderoid.soap.classeCommune.StatutCommande;
import orderoid.soap.classeCommune.StatutProduit;
import orderoid.soap.classeCommune.TypeEmploye;
import orderoid.soap.classeCommune.TypeProduit;

import org.ksoap2.serialization.SoapObject;

public class DeserialisationTool {
	
	public DeserialisationTool() {
		super();
	}
	
	//SoapObject -> client
	public static Client deserialisationClient(SoapObject soapObjectResp){
		Client client = new Client();
		
		if (soapObjectResp.hasProperty("idClient")) {
			client.setIdClient(Integer.valueOf(soapObjectResp
					.getPropertyAsString("idClient")));
		}
		
		if (soapObjectResp.hasProperty("nom")) {
			client.setNom(soapObjectResp.getPropertyAsString("nom"));
		}
		
		if (soapObjectResp.hasProperty("prenom")) {
			client.setPrenom(soapObjectResp.getPropertyAsString("prenom"));
		}
		
		if (soapObjectResp.hasProperty("telephone")) {
			client.setTelephone(soapObjectResp.getPropertyAsString("telephone"));
		}
		
		if (soapObjectResp.hasProperty("password")) {
			client.setPassword(soapObjectResp.getPropertyAsString("password"));
		}
		
		if (soapObjectResp.hasProperty("email")) {
			client.setEmail(soapObjectResp.getPropertyAsString("email"));
		}
		
		if (soapObjectResp.hasProperty("idCivilite")) {
			SoapObject civilite = (SoapObject) soapObjectResp
					.getProperty("idCivilite");
			Civilite civ = new Civilite();
			civ.setIdCivilite(Integer.valueOf(civilite
					.getPropertyAsString("idCivilite")));
			//civ.setLibelle(civilite.getPropertyAsString("libelle"));
			client.setIdCivilite(civ);
			client.setNom(soapObjectResp.getPropertyAsString("nom"));
		}
		
		return client;
	}
	
	public static Civilite deserialisationCivilite(SoapObject so)
	{
		Civilite c = new Civilite();
		
		if ( so.hasProperty("idCivilite"))
		{
			c.setIdCivilite(Integer.valueOf(so.getPropertyAsString("idCivilite")));
		}
		
		if ( so.hasProperty("libelle"))
		{
			c.setLibelle(so.getPropertyAsString("libelle"));
		}
		
		return c; 
	}
	
	public static Adresse deserialisationAdresse(SoapObject so)
	{
		Adresse a = new Adresse();
		
		if ( so.hasProperty("idAdresse"))
		{
			a.setIdAdresse(Integer.valueOf(so.getPropertyAsString("idAdresse")));
		}
		
		if ( so.hasProperty("adresse"))
		{
			a.setAdresse(so.getPropertyAsString("adresse"));
		}
		
		if (so.hasProperty("idClient")) 
		{
			SoapObject soClient = (SoapObject) so.getProperty("idClient");
			Client c = deserialisationClient(soClient);

			a.setIdClient(c);
		}
		
		if (so.hasProperty("adresseComplement"))
		{
			if(!so.getPropertyAsString("adresseComplement").startsWith("anyType"))
			{
				a.setAdresseComplement(so.getPropertyAsString("adresseComplement"));
			}
		}
		
		if ( so.hasProperty("codePostal"))
		{
			a.setCodePostal(so.getPropertyAsString("codePostal"));
		}
		
		if ( so.hasProperty("ville")){
			a.setVille(so.getPropertyAsString("ville"));
		}
		
		if ( so.hasProperty("commandeCollection"))
		{
			SoapObject soCommandes = (SoapObject) so.getProperty("commandeCollection");
			
			List<Commande> commandes = new ArrayList<Commande>();
			
			for ( int i = 0 ; i < soCommandes.getPropertyCount() ; ++i )
			{
				SoapObject soC = (SoapObject) soCommandes.getProperty(i);
				
				if ( soC.hasProperty("idCommande") ) 
				{
					commandes.add(deserialisationCommande(soC));
				}
			}
			
			a.setCommandeCollection(commandes); 
		}
		
		return a; 
	}
	
	public static StatutCommande deserialisationStatutCommande(SoapObject so)
	{
		StatutCommande s = new StatutCommande();
		
		if ( so.hasProperty("idStatut"))
		{
			s.setIdStatut(Integer.valueOf(so.getPropertyAsString("idStatut")));
		}
		
		if ( so.hasProperty("libelle"))
		{
			s.setLibelle(so.getPropertyAsString("libelle"));
		}
		
		return s;
	}
	
	// DONE
	public static Restaurant deserialisationRestaurant(SoapObject so)
	{
		Restaurant r = new Restaurant();
		
		if ( so.hasProperty("idRestaurant"))
		{
			r.setIdRestaurant(Integer.valueOf(so.getPropertyAsString("idRestaurant")));
		}
		
		if ( so.hasProperty("nom"))
		{
			r.setNom(so.getPropertyAsString("nom"));
		}
		
		if ( so.hasProperty("adresse")) 
		{
			r.setAdresse(so.getPropertyAsString("adresse"));
		}
		
		if ( so.hasProperty("telephone"))
		{
			r.setTelephone(so.getPropertyAsString("telephone"));
		}
		
		if ( so.hasProperty("fax"))
		{
			r.setFax(so.getPropertyAsString("fax"));
		}
		
		if ( so.hasProperty("codePostal"))
		{
			r.setCodePostal(so.getPropertyAsString("codePostal"));
		}
		
		if ( so.hasProperty("ville"))
		{
			r.setVille(so.getPropertyAsString("ville"));
		}
		
		if ( so.hasProperty("idMb"))
		{
			SoapObject soMB = (SoapObject) so.getProperty("idMb");
			MarqueBlanche m = deserialisationMarqueBlanche(soMB);
			r.setIdMb(m);
		}
		
		return r;
	}
	
	// DONE
	public static MarqueBlanche deserialisationMarqueBlanche (SoapObject so)
	{
		MarqueBlanche m = new MarqueBlanche();
		
		if ( so.hasAttribute("idMb"))
		{
			m.setIdMb(Integer.valueOf(so.getPropertyAsString("idMb")));
		}
		
		if ( so.hasAttribute("raisonSociale"))
		{
			m.setRaisonSociale(so.getPropertyAsString("raisonSociale"));
		}
		
		if ( so.hasAttribute("numRcs"))
		{
			m.setNumRcs(so.getPropertyAsString("numRcs"));
		}
		
		if ( so.hasAttribute("numSiret"))
		{
			m.setNumSiret(so.getPropertyAsString("numSiret"));
		}
		
		if ( so.hasAttribute("codeApe"))
		{
			m.setCodeApe(so.getPropertyAsString("codeApe"));
		}
		
		return m; 
	}
	
	// DONE
	public static Commande deserialisationCommande(SoapObject so)
	{
		Commande c = new Commande();
		
		if ( so.hasProperty("idCommande"))
		{
			c.setIdCommande(Integer.valueOf(so.getPropertyAsString("idCommande")));
		}
		
		if ( so.hasProperty("idRestaurant"))
		{
			SoapObject soRest = (SoapObject) so.getProperty("idRestaurant");
			Restaurant r = deserialisationRestaurant(soRest); 
			c.setIdRestaurant(r); 
		}
		
		if ( so.hasProperty("idClient"))
		{
			SoapObject soClient = (SoapObject) so.getProperty("idClient"); 
			Client client = deserialisationClient(soClient); 
			c.setIdClient(client); 
		}
		
		if ( so.hasProperty("idAdresse"))
		{
			SoapObject soAd = (SoapObject) so.getProperty("idAdresse"); 
			Adresse a = deserialisationAdresse(soAd); 
			c.setIdAdresse(a); 
		}
		
		if ( so.hasProperty("idStatut"))
		{
			SoapObject soStatut = (SoapObject) so.getProperty("idStatut"); 
			StatutCommande s = deserialisationStatutCommande(soStatut); 
			c.setIdStatut(s); 
		}

		if ( so.hasProperty("dateCommande"))
		{
			c.setDateCommande(so.getPropertyAsString("dateCommande"));
		}
		
		if ( so.hasProperty("dateLivraison"))
		{
			c.setDateLivraison(so.getPropertyAsString("dateLivraison"));
		}
		
		if ( so.hasProperty("montant"))
		{
			c.setMontant(Double.parseDouble(so.getPropertyAsString("montant")));
		}
		
		if ( so.hasProperty("raisonRefus"))
		{
			c.setRaisonRefus(so.getPropertyAsString("raisonRefus"));
		}
		
	//	if ()
		
		return c; 
	}
	
	// DONE
	public static TypeEmploye deserialisationTypeEmploye ( SoapObject so )
	{
		TypeEmploye t = new TypeEmploye(); 
		
		if ( so.hasProperty("idTypeEmploye"))
		{
			t.setIdTypeEmploye(Integer.valueOf(so.getPropertyAsString("idTypeEmploye")));
		}
		
		if ( so.hasProperty("libelle"))
		{
			t.setLibelle(so.getPropertyAsString("libelle"));
		}
		
		return t; 
	}
	
	// DONE
	public static Employe deserialisationEmploye(SoapObject so)
	{
		Employe e = new Employe(); 
		
		if ( so.hasProperty("idEmploye"))
		{
			e.setIdEmploye(Integer.valueOf(so.getPropertyAsString("idEmploye")));
		}
		
		if ( so.hasProperty("idRestaurant"))
		{
			SoapObject soResto = (SoapObject) so.getProperty("idRestaurant"); 
			Restaurant r = deserialisationRestaurant(soResto); 
			e.setIdRestaurant(r); 
		}
		
		if ( so.hasProperty("login"))
		{
			e.setLogin(so.getPropertyAsString("login"));
		}
		
		if ( so.hasProperty("password"))
		{
			e.setPassword(so.getPropertyAsString("password"));
		}
		
		if ( so.hasProperty("nom"))
		{
			e.setNom(so.getPropertyAsString("nom"));
		}
		
		if ( so.hasProperty("prenom"))
		{
			e.setPrenom(so.getPropertyAsString("prenom"));
		}
		
		if ( so.hasProperty("dateNaissance"))
		{
			e.setDateNaissance(so.getPropertyAsString("dateNaissance"));
		}
		
		if ( so.hasProperty("dateDebutContrat"))
		{
			e.setDateDebutContrat(so.getPropertyAsString("dateDebutContrat"));
		}
		
		if ( so.hasProperty("dateFinContrat"))
		{
			e.setDateFinContrat(so.getPropertyAsString("dateFinContrat"));
		}
		
		if ( so.hasProperty("telephone"))
		{
			e.setTelephone(so.getPropertyAsString("telephone"));
		}
		
		if ( so.hasProperty("idTypeEmploye"))
		{
			SoapObject soTypeE = (SoapObject) so.getProperty("idTypeEmploye");
			TypeEmploye t = deserialisationTypeEmploye(soTypeE);
			e.setIdTypeEmploye(t);
		}
		
		return e; 
	}

	// DONE
	public static StatutProduit deserialisationStatutProduit(SoapObject so)
	{
		StatutProduit s = new StatutProduit(); 
		
		if ( so.hasProperty("idStatut"))
		{
			s.setIdStatut(Integer.valueOf(so.getPropertyAsString("idStatut")));
		}
		
		if ( so.hasProperty("libelle"))
		{
			s.setLibelle(so.getPropertyAsString("libelle"));
		}
		
		return s; 
	}

	// DONE
	public static TypeProduit deserialisationTypeProduit(SoapObject so)
	{
		TypeProduit t = new TypeProduit(); 
		
		if ( so.hasProperty("idType"))
		{
			t.setIdType(Integer.valueOf(so.getPropertyAsString("idType")));
		}
		
		if ( so.hasProperty("libelle"))
		{
			t.setLibelle(so.getPropertyAsString("libelle"));
		}
		
		return t; 
	}

	// DONE
	public static Produit deserialisationProduit(SoapObject so)
	{
		Produit p = new Produit(); 
		
		if ( so.hasProperty("idProduit"))
		{
			p.setIdProduit(Integer.valueOf(so.getPropertyAsString("idProduit")));
		}
		
		if ( so.hasProperty("idStatut"))
		{
			SoapObject soStatut = (SoapObject) so.getProperty("idStatut");
			StatutProduit s = deserialisationStatutProduit(soStatut); 
			p.setIdStatut(s); 
		}

		if ( so.hasProperty("montant"))
		{
			p.setMontant(Double.parseDouble(so.getPropertyAsString("montant"))); 
		}
		
		if ( so.hasProperty("nom"))
		{
			p.setNom(so.getPropertyAsString("nom"));
		}
		
		if ( so.hasProperty("promotion"))
		{
			p.setPromotion(Double.parseDouble(so.getPropertyAsString("promotion")));
		}
		
		if ( so.hasProperty("quantite"))
		{
			p.setQuantite(Integer.valueOf(so.getPropertyAsString("quantite")));
		}
		
		if ( so.hasProperty("idType"))
		{
			SoapObject soT = (SoapObject) so.getProperty("idType");
			TypeProduit t = deserialisationTypeProduit(soT);
			p.setIdType(t);
		}
		
		if ( so.hasProperty("dateLivraison"))
		{
			p.setDateLivraison(so.getPropertyAsString("dateLivraison"));
		}
		
		if ( so.hasProperty("dateFin"))
		{
			p.setDateFin(so.getPropertyAsString("dateFin"));
		}
		
		return p; 
	}

	// DONE
	public static Menu deserialisationMenu ( SoapObject so)
	{
		Menu m = new Menu(); 
		
		if ( so.hasProperty("idMenu"))
		{
			m.setIdMenu(Integer.valueOf(so.getPropertyAsString("idMenu")));
		}
		
		if ( so.hasProperty("nom"))
		{
			m.setNom(so.getPropertyAsString("nom")); 
		}
		
		if ( so.hasProperty("montant"))
		{
			m.setMontant(Double.parseDouble(so.getPropertyAsString("montant")));
		}
		
		if ( so.hasProperty("promotion"))
		{
			m.setPromotion(Double.parseDouble(so.getPropertyAsString("promotion")));
		}
		
		if ( so.hasProperty("dateDebut"))
		{
			m.setDateDebut(so.getPropertyAsString("dateDebut"));
		}
		
		if ( so.hasProperty("dateFin"))
		{
			m.setDateFin(so.getPropertyAsString("dateFin"));
		}
		
		return m; 
	}

	public static Boolean deserialisationBoolean(SoapObject so)
	{
		Boolean bool = false;
		//TODO recuperé la valeur qui va passé, je sais pas comment passe un booleen ...
		if ( so.hasProperty("idCivilite"))
		{
			//c.setIdCivilite(Integer.valueOf(so.getPropertyAsString("idCivilite")));
		}
		return bool;
	}
	
}
